package com.lhl.jwt.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.lhl.api.annotation.IgnorReponseAdvice;
import com.lhl.jwt.dto.WxUserInfo;
import com.lhl.jwt.utils.JwtConstant;
import com.lhl.wx.cp.autoconfigure.WxCpConfiguration;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @className: com.lhl.jwt.controller.WxPortalController
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-12-29 11:25
 **/
@Api(tags = "WxPortalController", produces = "WxPortalController")
@RestController
@Slf4j
@RequestMapping("/userInfo/{agentId}")
public class WxPortalController {

    @GetMapping(produces = "text/plain;charset=utf-8")
    public void getUserInfo(@PathVariable Integer agentId, @RequestParam String code, HttpServletResponse response) throws WxErrorException, IOException {
        log.debug("-----------------------------------------------------------");
        log.debug("code == {}",code);
        WxUserInfo wxUserInfo = new WxUserInfo();
        WxCpOauth2UserInfo wxCpOauth2UserInfo = WxCpConfiguration.getCpService(agentId).getOauth2Service().getUserInfo(code);
        log.debug("wxCpOauth2UserInfo == {}", JSONUtil.toJsonStr(wxCpOauth2UserInfo));
        WxCpUser user = WxCpConfiguration.getCpService().getUserService().getById(wxCpOauth2UserInfo.getUserId());
        log.debug("user==={}", JSONUtil.toJsonPrettyStr(user));
        String token = getToken(user);
        response.setHeader(JwtConstant.AUTHORIZATION_HEADER, token);
        response.setHeader(JwtConstant.ACCESS_CONTROL_EXPOSE_HEADER, JwtConstant.AUTHORIZATION_HEADER);
        response.getWriter().write(JSONUtil.toJsonStr(user));
    }

    @GetMapping("/test")
    @IgnorReponseAdvice
    public void getUserInfoTest(@PathVariable Integer agentId, @RequestParam String userId,
            @RequestParam String mobile,HttpServletResponse response) throws WxErrorException, IOException {
        log.debug("-----------------------------------------------------------");
        WxCpUser user= new WxCpUser();
        user.setUserId(userId);
        user.setMobile(mobile);
        String token = getToken(user);
        response.setHeader(JwtConstant.AUTHORIZATION_HEADER, token);
        response.setHeader(JwtConstant.ACCESS_CONTROL_EXPOSE_HEADER, JwtConstant.AUTHORIZATION_HEADER);
        response.getWriter().write(JSONUtil.toJsonStr(user));
    }

    private String getToken(WxCpUser user){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("wxId", user.getUserId());
        paramMap.put("mobile", user.getMobile());
        paramMap.put("type", "cp");
        HttpResponse res = HttpRequest.post("http://wxapi.qipai.com.cn/cp/wxLogin").form(paramMap).execute();
        log.debug("res=={}",res);
        log.debug("Authorization=={}",res.header("Authorization"));
        return res.header("Authorization");
    }
}
